<?php
 class ActivityController extends Controller{
  public $isActivated = 0;
          public function accessRules()
		 {
			return array(
			   array(
					'allow',  // allow all users to access 'index' and 'view' actions.
					'expression'=>'$user->rights>=2',
				),
				array(
					'deny',  // deny all users
					'users'=>array('*'),
					'message'=>'You do not have access to this page ! ! ',
				),
				
			);
		 } 
     public function actionIndex(){
		 $this->breadcrumbs=array(
               '学生报名'=>array('index'),
               '所有学生报名',
             );
			
		  $ci = new CDbCriteria;
		  $acs = Activity::model()->findAll();
		  $total = Activity::model()->count($ci);
		  $pages =  new CPagination($total);
		  $pages->pageSize = 40;
		  $pages->applyLimit($ci);
		  $users = Activity::model()->findAll($ci);
		 $this->render("index",array('acs'=>$acs,'pages'=>$pages));
		 
	 }
	  public function actionAddactivity(){
		  $this->breadcrumbs=array(
               '学生报名'=>array('index'),
               '添加学生报名',
             );
		 if(Yii::app()->request->requestType == 'POST'){
			 
			 $this->addActivityDeal();
			 
			 
		 }
		 
		 $this->render('addactivity');
		 
	 }
	 private function addActivityDeal(){
		 $request = Yii::app()->request;
		 $ac = new Activity;
		 $ac->name = $request->getParam('name');
		 $ac->stunum = $request->getParam('stunum');
		 $ac->attendnum = 0;
		 $ac->content = nl2br($request->getParam('content')); 
		 $ac->requirement = nl2br($request->getParam('requirement'));
		 $ac->addtime = date('Y-m-d H:i:s');
		 if($ac->save()){
			 Yii::app()->user->setFlash('add','添加成功，请到已添加的学生报名查看.');
			 
		 }else{
			 $this->logAr($ac);
			  Yii::app()->user->setFlash('add','添加失败');
		 }
		  $this->redirect(array('activity/addactivity'));
	 }
	 public function actionDelete(){
		 if(Yii::app()->request->isAjaxRequest){
			 $request = Yii::app()->request;
			 $activityId  = $request->getParam('id');
			 $ac = Activity::model()->findByPk($activityId);
		     if($ac){
				 if($ac->delete()){
					 echo json_encode(array('msg'=>'删除成功','code'=>200));
					 
				 }else{
					 $this->logAr($ac);
					 echo json_encode(array('msg'=>'删除失败','code'=>504));
					 
				 }
			 }else{
				 echo json_encode(array('msg'=>'没有要找到这个条目','code'=>404));
				 
			 }
			 
		 }else{
			 $this->redirect(array('activity/index'));
			 
		 }
		 
	 }
	 public function actionCheckactivity(){
		 
		 $acs = Activity::model()->findAll();
		 
		 $this->render("checkactivity",array('acs'=>$acs));
		 
	 }
	  // 审核学生活动的学生列表
	  public function actionActivitylist(){
		 $request = Yii::app()->request;
		 $ac = Activity::model()->findByPk(Yii::app()->request->getParam('id'));
		 if($ac){
           	$this->breadcrumbs=array(
               '学生报名'=>array('index'),
               '审核 '.$ac->name.' 活动的学生报名',
             );	
			  $classRooms = Classroom::model()->findAll();
			  $ci  = new CDbCriteria();
			  $major = Major::model()->findAll();
			 if(!empty($_GET) && (count($_GET) >= 5)){
			  foreach($_GET as $key => $value){
				  if($key == 'id' || $key == 'page')
					  continue;
				  if($value == '')
					  continue;
				  if($key == 'stuNumber'){
					  $ci->params[':s'] = $_GET['stuNumber'];
					  $ci->addCondition('u.username = :s');		  
				  }else{
				   $p = ':'.substr($key,0,1);	  
				   $ci->params[$p] = $value;
				   $ci->addCondition('stuinfo.'.$key.' = '.$p);  
				  }
				  
			  }
			  $ci->with = array('u','stuinfo');
			 }
			 $ci->addCondition('activityId = '.$ac->activityId);
			 $ci->addCondition('t.condition = 1'); // 1 ---- 未审核 2 ---- 未通过 3 --- 已通过 
			
			 
			 $total = Stuactivity::model()->count($ci);
			 $pages =  new CPagination($total);
			 $pages->pageSize = 40;
			 $pages->applyLimit($ci);
        	
		   $stuAcs = Stuactivity::model()->findAll($ci);
		   
		  
		 }else{
			 $this->redirect(array('activity/checkactivity'));
			 
		 }
		 $this->render("activitylist",array('major'=>$major,'stuAcs'=>$stuAcs,'ac'=>$ac,'classRooms'=>$classRooms,'total'=>$total,'pages'=>$pages));
		 
	 }
	 
	 public  function actionActivityreport(){
		 
		  $request = Yii::app()->request;
		  $ac = Activity::model()->findByPk(Yii::app()->request->getParam('id'));
		 if($ac){
           	$this->breadcrumbs=array(
               '学生报名'=>array('index'),
               $ac->name.' 活动报表',
             );	
			  $classRooms = Classroom::model()->findAll();
			  $ci  = new CDbCriteria();
			  $major = Major::model()->findAll();
			 if(!empty($_GET) && (count($_GET) >= 5)){
			  foreach($_GET as $key => $value){
				  if($key == 'id' || $key == 'page')
					  continue;
				  if($value == '')
					  continue;
				  if($key == 'stuNumber'){
					  $ci->params[':s'] = $_GET['stuNumber'];
					  $ci->addCondition('u.username = :s');		  
				  }else{
				   $p = ':'.substr($key,0,1);	  
				   $ci->params[$p] = $value;
				   $ci->addCondition('stuinfo.'.$key.' = '.$p);  
				  }
				  
			  }
			  $ci->with = array('u','stuinfo');
			 }
			 $ci->addCondition('activityId = '.$ac->activityId);
			 $ci->addCondition('t.condition = 3'); // 1 ---- 未审核 2 ---- 未通过 3 --- 已通过 
			
			 
			 $total = Stuactivity::model()->count($ci);
			 $pages =  new CPagination($total);
			 $pages->pageSize = 40;
			 $pages->applyLimit($ci);
        	
		      $stuAcs = Stuactivity::model()->findAll($ci);
		   
		  
		 }else{
			 $this->redirect(array('activity/checkactivity'));
			 
		 }
		 $this->render("activityreport",array('major'=>$major,'stuAcs'=>$stuAcs,'ac'=>$ac,'classRooms'=>$classRooms,'total'=>$total,'pages'=>$pages));
		 
		 
		 
		 
	 }
	  public function actionAddcomment(){
		   if(Yii::app()->request->isAjaxRequest){
			   $request = Yii::app()->request;
			   $stuAc  = Stuactivity::model()->findByPk($request->getParam('stuActivityId'));
			   $comment = $request->getParam('comment');
			   if($stuAc){
			   $stuN = new Stunotification;
			   $stuN->uId = $stuAc->uId;
			   $stuN->detailId = $stuAc->stuActivityId;
			   $stuN->module = 5; //1 -- 个人信息 2 -- 综合奖学金 3 -- GSP 4---综合素质 5 -- 学生报名
			   $stuN->comment = $comment;
			   $stuN->addtime = date('Y-m-d H:i:s');
			   $stuN->isRead = 0;
			   $stuAc->condition = 2 ;// 1 ---- 未审核 2 ---- 未通过 3 --- 已通过
			   
			   if($stuN->save()&&$stuAc->save()){
				  
				  echo  json_encode(array('code'=>200,'msg'=>'添加成功'));
			   
			   }else{
					$this->logAr($stuN);
					$this->logAr($stuAc);
					echo  json_encode(array('code'=>502,'msg'=>'添加失败'));
				}
			   
			   
			   }else{
				  echo  json_encode(array('code'=>404,'msg'=>'没有找到这个条目'));
				   
			   }
		   }else{
			   $this->redirect(array('activity/activitylist'));
			   
		   }
		   
	   }
	  public function actionPassone(){
		 if(Yii::app()->request->isAjaxRequest){
			   $stuAc = Stuactivity::model()->findByPk(Yii::app()->request->getParam('stuActivityId'));
			   if($stuAc){
				   $stuAc->condition = 3 ; // 1 ---- 未审核 2 ---- 未通过 3 --- 已通过
				   if($stuAc->save()){
						
						echo  json_encode(array('code'=>200,'msg'=>' 审核成功'));
						
				   }else{
					   $this->logAr($stuAc);
					   echo  json_encode(array('code'=>502,'msg'=>'审核失败'));
				   }
			   }else{
				   echo  json_encode(array('code'=>404,'msg'=>'审核失败，没有找到用户'));
				   
			   }
			   
		   }else{
			   $this->logError('非法请求，activity 审核 ajax');
			   $this->redirect(array('activity/activitylist'));
			   
		   }
		   
		  
	  }
	   public function actionPassmuti(){
		   $successNum = 0;
		   $successArray = array();
		   if(Yii::app()->request->isAjaxRequest){
			   $stus = '&'.$_POST['stus'];
			   $stuArray = explode('&l=',$stus);
			  foreach($stuArray as $stuActivityId){
				  if($stuActivityId == '')
					  continue;
			   $stuAc = Stuactivity::model()->findByPk($stuActivityId);
			   if($stuAc){
				   $stuAc->condition = 3; // 1 ---- 未审核 2 ---- 未通过 3 --- 已通过
				   if($stuAc->save()){
					    $successArray[$successNum] = $stuActivityId;
						$successNum++;
				    }else{
					   $this->logAr($stuAc);
					   echo  json_encode(array('code'=>502,'msg'=>' 审核失败'.'<br>成功的学生数目：'.$successNum,'successArray'=>$successArray));
					   return;
				   }
			    }else{
				   echo  json_encode(array('code'=>404,'msg'=>'审核失败，没有找到用户'.'<br>成功的学生数目：'.$successNum,'successArray'=>$successArray));
				   return;
			    }
			  }
		   echo  json_encode(array('code'=>200,'msg'=>'审核成功的学生数目：'.$successNum,'successArray'=>$successArray));
		   }else{
			   $this->logError('非法请求，activity 多位 学生审核 ajax');
			   $this->redirect(array('activity/activitylist'));
			   
		   }
		   
	   }
	   public function actionUnpassmuti(){
		   $successNum = 0;
		   $successArray = array();
		   if(Yii::app()->request->isAjaxRequest){
			   $stus = '&'.$_POST['stus'];
			   $stuArray = explode('&l=',$stus);
			  foreach($stuArray as $stuActivityId){
				  if($stuActivityId == '')
					  continue;
			   $stuAc = Stuactivity::model()->findByPk($stuActivityId);
			   if($stuAc){
				   $stuN = new Stunotification;
				   $stuN->uId = $stuAc->uId;
				   $stuN->detailId = $stuAc->stuActivityId;
				   $stuN->module = 5; //1 -- 个人信息 2 -- 综合奖学金 3 -- GSP 4---综合素质 5 -- 学生报名
				   $stuN->comment = '同学，您的这一条学生活动没有通过审核，请删除后，再次提交';
				   $stuN->addtime = date('Y-m-d H:i:s');
				   $stuN->isRead = 0;
				   
				   $stuAc->condition = 2; // 1 ---- 未审核 2 ---- 未通过 3 --- 已通过
				   if($stuAc->save()&&$stuN->save()){
					    $successArray[$successNum] = $stuActivityId;
						$successNum++;
				    }else{
					   $this->logAr($stuAc);
					   echo  json_encode(array('code'=>502,'msg'=>' 审核失败'.'<br>成功的学生数目：'.$successNum,'successArray'=>$successArray));
					   return;
				   }
			    }else{
				   echo  json_encode(array('code'=>404,'msg'=>'添加不通过审核失败，没有找到用户'.'<br>成功的学生数目：'.$successNum,'successArray'=>$successArray));
				   return;
			    }
			  }
		   echo  json_encode(array('code'=>200,'msg'=>'添加不通过审核成功的学生数目：'.$successNum,'successArray'=>$successArray));
		   }else{
			   $this->logError('非法请求，activity 多位 学生审核 ajax');
			   $this->redirect(array('activity/activitylist'));
			   
		   }
		   
	   }
	   
	     public function actionCloseactivity(){
			 
			 
		   if(Yii::app()->request->isAjaxRequest){
			   $request = Yii::app()->request;
			   $Ac  = Activity::model()->findByPk($request->getParam('id'));
			   if($Ac){
					 
					 if($Ac->isclosed === '0'){
						 
						 $Ac->isclosed = 1;
						 
						 if($Ac->save()){
							 
							 echo  json_encode(array('code'=>200,'msg'=>'活动关闭成功'));
						 
						 }else{
							  
							  $this->logAr($Ac);
							  echo  json_encode(array('code'=>502,'msg'=>'活动关闭失败'));
							 
						 }
					 }else{
					      
						  echo  json_encode(array('code'=>404,'msg'=>'这个活动已经关闭'));
					 
					 }
					 
			   
			   }else{
				  echo  json_encode(array('code'=>404,'msg'=>'没有找到这个活动'));
				   
			   }
		   }else{
			   $this->redirect(array('activity/activitylist'));
			   
		   }
		   
		   
	   }
	   
	   public function actionStartactivity(){
			 
			 
		   if(Yii::app()->request->isAjaxRequest){
			   $request = Yii::app()->request;
			   $Ac  = Activity::model()->findByPk($request->getParam('id'));
			   if($Ac){
					 
					 if($Ac->isclosed === '1'){
						 // 开启活动
						 $Ac->isclosed = 0;
						 
						 if($Ac->save()){
							 
							 echo  json_encode(array('code'=>200,'msg'=>'活动开启成功'));
						 
						 }else{
							  
							  $this->logAr($Ac);
							  echo  json_encode(array('code'=>502,'msg'=>'活动开启失败'));
							 
						 }
					 }else{
					      
						  echo  json_encode(array('code'=>404,'msg'=>'这个活动没有被关闭'));
					 
					 }
					 
			   
			   }else{
				  echo  json_encode(array('code'=>404,'msg'=>'没有找到这个活动'));
				   
			   }
		   }else{
			   $this->redirect(array('activity/activitylist'));
			   
		   }
		   
		   
	   }
	   
	   
	   public function  actionExportexcel(){
		   $request = Yii::app()->request;
		  $ac = Activity::model()->findByPk(Yii::app()->request->getParam('activityId'));
		 if($ac){
           	
			  $classRooms = Classroom::model()->findAll();
			  $ci  = new CDbCriteria();
			  $major = Major::model()->findAll();
			 if(!empty($_GET) && (count($_GET) >= 5)){
			  foreach($_GET as $key => $value){
				  if($key == 'activityId' || $key == 'page')
					  continue;
				  if($value == '')
					  continue;
				  if($key == 'stuNumber'){
					  $ci->params[':s'] = $_GET['stuNumber'];
					  $ci->addCondition('u.username = :s');		  
				  }else{
				   $p = ':'.substr($key,0,1);	  
				   $ci->params[$p] = $value;
				   $ci->addCondition('stuinfo.'.$key.' = '.$p);  
				  }
				  
			  }
			  $ci->with = array('u','stuinfo');
			 }
			 $ci->addCondition('activityId = '.$ac->activityId);
			 $ci->addCondition('t.condition = 3'); // 1 ---- 未审核 2 ---- 未通过 3 --- 已通过 
			
			 $total = Stuactivity::model()->count($ci);
			 $stuAcs = Stuactivity::model()->findAll($ci);
		    
		  Yii::$enableIncludePath = false;
		  Yii::import('application.extensions.phpExcel.PHPExcel',1);
		  $objPHPExcel = new PHPExcel();
		  $objPHPExcel->getProperties()->setCreator("kezhi Company")
							 ->setLastModifiedBy("第二师范学院学生信息管理系统")
							 ->setTitle(date('Y-m-d H:i:s').'学生GSP导出')
							 ->setSubject("学生活动信息导出")
							 ->setDescription("学生活动信息导出")
							 ->setKeywords("学生活动信息导出")
							 ->setCategory("export activity file");
		 
		  $objPHPExcel->setActiveSheetIndex(0)
					->setCellValue('A1', '学号')
					->setCellValue('B1', '姓名')
					->setCellValue('C1', '年级')
					->setCellValue('D1', '班级')
					->setCellValue('E1', '专业')
					->setCellValue('F1', '学生添加内容')
					->setCellValue('G1', '添加时间');
					
		$i = 2;			
		 foreach($stuAcs as $u){
			
			 if($u->stuinfo){
				     $realName = $u->stuinfo->realName;
					 $grade = $u->stuinfo->grade;
					 $classRoom = $u->stuinfo->classRoom;
					 $major = $u->stuinfo->major; 
				    
			 }else{
				     $realName = '未填写';
					 $grade = '未填写';
					 $classRoom = '未填写';
					 $major = '未填写'; 
				 
			 }
			$objPHPExcel->setActiveSheetIndex(0)
					->setCellValue('A'.$i, ' '.$u->u->username)
					->setCellValue('B'.$i, $realName)
					->setCellValue('C'.$i, $grade)
					->setCellValue('D'.$i, $classRoom)
					->setCellValue('E'.$i, $major)
					->setCellValue('F'.$i, $u->detail)
					->setCellValue('G'.$i, ' '.$u->addtime);
					
					$i++;		
			
		 }			
					
		 $this->downloadExcel(date('Y-m-d').'活动名称 : '.$ac->name.' 参加人数 ：('.$total.')',$objPHPExcel);
		  
		 }else{
			 $this->redirect(array('activity/checkactivity'));
			 
		 }
		
	}
     
}
?>